/**
 * 房間操作類
 */
function roomAc() {
	/**
	 * 擴展功能，注意通用參數
	 */
	if ( roomAc.prototype.handleFun == undefined ) {
		roomAc.prototype.handleFun = function(r_room, r_user, r_params) {
			if (r_user == null) {
				var _param = { cmd:'roomAc', err:r_params['ac'] };
				return errEvent(r_room, r_user, _param, 0);	//一般來說怎麼會出現用戶丟失呢				
			}
			if (r_room == null || r_room == undefined || r_room == -1) {
				var _ac = r_params.ac;
				if (_ac != 'getRoomList' && _ac != 'joinRoom' ) {
					r_room = _zone.getRoom((r_user.getRoomsConnected())[0]);
				}
			}			
			return this[r_params.ac](r_room,r_user,r_params);
		}		
	}	
}

/**
 * 獲取房間列表
 */
roomAc.prototype.getRoomList = function(r_room, r_user, r_params) {
	var _error = 0;
	var _params = { };
	_params._cmd = 'getRoomList';
	if (typeof(r_params) != 'object') {
		_params.dataObj = { err:7000 };
	}else {
		var _room_list = __roomEvent.getRoomList(r_params['type']);
		if (_room_list == null) {
			_params.dataObj = { err:7001 };
		}else {
			_params.dataObj = { data:_room_list ,type:r_params.type};
		}		
	}
	_server.sendResponse(_params, -1, null, [r_user], 'json');	
}

/**
 * 加入指定房間
 */
roomAc.prototype.joinRoom = function(r_room,r_user,r_params) {
	var _params = { };	
	if (parseInt(r_params.rid) < 0) {	
		_params._cmd = "joinRoom";	
		_params.dataObj = { err:7003 };
		_server.sendResponse(_params, -1, null, [r_user], 'json');	
		return;
	}			
	var _room_temp = __roomEvent.joinRoom(r_user, { roomId:r_params.rid, pwd:'' } );				
	
	//进入房间出错
	if ("err" in _room_temp) {
		_params.dataObj = { err:_room_temp.err };		
		_server.sendResponse(_params, -1, null, [r_user], 'json');		
		__log.show(_room_temp);	
		return;
	}
	
	__userEvent.setUserState(_room_temp, r_user, "spectator");  //設置玩家状态
	
	//通知其他玩家
	var _user_list = _room_temp.getAllUsersButOne(r_user.getUserId());	//获取玩家，除去自己
	var _room_vars = __roomEvent.getJoinRoomInfo(_room_temp);  //房間信息	
	var _param = { };
	_param._cmd = "userJoin";
	_param.dataObj = { u:__userVars.getUserVars(r_user)};
	_server.sendResponse(_param, -1, null, _user_list, 'json');
	
	//发送房间中其他玩家数据，游戏数据给新进入房间玩家
	var _list = _room_temp.getAllUsers();
	var _data = { };	
	_data.u = __userEvent.getUserListVars(_list);	
	_data.r = __roomEvent.getJoinRoomInfo(_room_temp); 
	_data.g = __roomEvent.getGameInfo(_room_temp);
	_data.err = 0;			
	_params.dataObj = _data;
	_params._cmd = "joinRoom";	
	_server.sendResponse(_params, -1, null, [r_user], 'json');			
}

/**
 * 選擇座位坐下
 * 初始房間indexArr
 */
roomAc.prototype.sitDown = function(r_room, r_user, r_params) {	
	var _type = __roomVars.getRoomType(r_room);		//获取房间类别	
	if (_type == 1) {  
		//比赛模式
		__roomEvent.raceRoomSitdown(r_room, r_user, r_params);
	}else {
		//普通模式
		__roomEvent.normalRoomSitdown(r_room, r_user, r_params);
	}
	return;	
}

/**
 * 玩家站起
 * r_params['sit']為當前座位序號
 */
roomAc.prototype.standUp = function(r_room, r_user, r_params) {
	var _params = { };
	_params._cmd = "standUp";	
	if (r_user.isSpectator()) {		
		return;  //玩家为观察者，直接返回	
	}
	//玩家站起时检查
	if (r_room.getAllPlayers().length <= 1 ) {		
		if(__roomVars.getState(r_room) == "start" ){  //正在进行游戏
			//.....到游戏中处理数据
		}else{
			__roomVars.setState(r_room, "free");
		}
	}	
	if (r_room.getAllPlayers().length <= 2 ) {
		if(__roomVars.getState(r_room) == "start" ){  //正在进行游戏
			//.....到游戏中处理数据
		}
	}
	
	var _sit = r_user.getPlayerIndex();				
	var _uid = parseInt(r_user.getName());	
	__userEvent.setUserState(r_room, r_user, 'spectator'); 	//设置玩家状态
	var _gold = __userEvent.exchangeGold(r_user);  			//将筹码退回到点数
	_params.dataObj = { sit:_sit, uid:_uid, gold:_gold };		
	_server.sendResponse(_params, -1, null, r_room.getAllUsers(), 'json');		
}

/**
 * 退出房間
 */
roomAc.prototype.exitRoom = function(r_room, r_user, r_params) {
	//执行站起操作
	this.standUp(r_room, r_user, r_params);		
	
	//发送玩家点数	
	var _params = { };
	_params._cmd = "upGold";	
	var _gold = __userVars.getVar(r_user, "gold", "int"); 
	_params.dataObj = {gold: _gold};
	_server.sendResponse(_params, -1, null, [r_user], 'json');		
		
	r_user.deleteVariable("bet");		
	__userEvent.setUserState(r_room, r_user, 'spectator');  	//修改玩家状态
	
	//退出房间
	_params._cmd = "exitRoom";	
	var _room = r_room;		
	var _uid = __userVars.getVar(r_user, "userId", "int");
	_params.dataObj = { uid:_uid, gold: _gold };
	_server.sendResponse(_params, -1, null, r_room.getAllUsers(), 'json');
	__roomEvent.exitRoom(r_user, _room);	

}
